﻿// ----------------------------------------------------------------------
// <copyright file="FieldToValueGetter.cs" company="Biwug">
//     Copyright statement. All right reserved
// </copyright>
//
// ------------------------------------------------------------------------

namespace Meligo.ModuleExtractor.GenerateModuleFactory.Helper
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using Meligo.ModuleExtractor.GenerateModuleFactory.Entities;
    using Microsoft.SharePoint;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class FieldToValueGetter
    {
        public static string FieldValueToString(SPField field, object value, Dictionary<string, PathSiteInfo> pathSiteInfoMap)
        {
            string result;
            if (value == null)
            {
                result = string.Empty;
            }
            else
            {
                if (field.FieldValueType == typeof(SPFieldUrlValue))
                {
                    SPFieldUrlValue sharePointFieldUrlValue = new SPFieldUrlValue(value.ToString());
                    if (string.IsNullOrEmpty(sharePointFieldUrlValue.Url))
                    {
                        result = value.ToString();
                    }
                    else
                    {
                        sharePointFieldUrlValue.Url = PropertyNodeGetter.GetSiteOrWebRelativeMingledUrl(field.ParentList.ParentWeb, sharePointFieldUrlValue.Url, pathSiteInfoMap);
                        result = sharePointFieldUrlValue.ToString();
                    }
                }
                else
                {
                    if (field.FieldValueType == typeof(DateTime))
                    {
                        if (value is DateTime)
                        {
                            result = ((DateTime)value).ToString("s", CultureInfo.InvariantCulture);
                            return result;
                        }
                    }

                    if (field.FieldValueType == typeof(double))
                    {
                        if (value is double)
                        {
                            result = ((double)value).ToString(CultureInfo.InvariantCulture);
                            return result;
                        }
                    }

                    result = value.ToString();
                }
            }

            return result;
        }
    }
}
